﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TicTacToe
{
    public partial class TicTacToe : Form
    {
        public TicTacToe()
        {
            getPlayers();
            InitializeComponent();
        }
        int vsComp;
        int turn = 1;
        int A1beenClicked = 0;
        int A2beenClicked = 0;
        int A3beenClicked = 0;
        int B1beenClicked = 0;
        int B2beenClicked = 0;
        int B3beenClicked = 0;
        int C1beenClicked = 0;
        int C2beenClicked = 0;
        int C3beenClicked = 0;
        private void A1_Click(object sender, EventArgs e)
        {
            if (A1beenClicked == 0)
            {
                if (turn % 2 == 0)
                    A1.Text = "O";
                else
                    A1.Text = "X";
                turn++;
                A1beenClicked = 1;
                if (vsComp == 1)
                {
                    computerChoice();
                }
            }

            if (turn > 9)
            {
                endOfGame();
            }
        }

        private void A2_Click(object sender, EventArgs e)
        {
            if (A2beenClicked == 0)
            {
                if (turn % 2 == 0)
                    A2.Text = "O";
                else
                    A2.Text = "X";
                turn++;
                A2beenClicked = 1;
                if (vsComp == 1)
                {
                    computerChoice();
                }
            }

            if (turn > 9)
            {
                endOfGame();
            }
        }

        private void A3_Click(object sender, EventArgs e)
        {
            if (A3beenClicked == 0)
            {
                if (turn % 2 == 0)
                    A3.Text = "O";
                else
                    A3.Text = "X";
                turn++;
                A3beenClicked = 1;
                if (vsComp == 1)
                {
                    computerChoice();
                }
            }

            if (turn > 9)
            {
                endOfGame();
            }
        }

        private void B1_Click(object sender, EventArgs e)
        {
            if (B1beenClicked == 0)
            {
                if (turn % 2 == 0)
                    B1.Text = "O";
                else
                    B1.Text = "X";
                turn++;
                B1beenClicked = 1;
                if (vsComp == 1)
                {
                    computerChoice();
                }
            }

            if (turn > 9)
            {
                endOfGame();
            }
        }

        private void B2_Click(object sender, EventArgs e)
        {
            if (B2beenClicked == 0)
            {
                if (turn % 2 == 0)
                    B2.Text = "O";
                else
                    B2.Text = "X";
                turn++;
                B2beenClicked = 1;
                if (vsComp == 1)
                {
                    computerChoice();
                }
            }

            if (turn > 9)
            {
                endOfGame();
            }
        }

        private void B3_Click(object sender, EventArgs e)
        {
            if (B3beenClicked == 0)
            {
                if (turn % 2 == 0)
                    B3.Text = "O";
                else
                    B3.Text = "X";
                turn++;
                B3beenClicked = 1;
                if (vsComp == 1)
                {
                    computerChoice();
                }
            }

            if (turn > 9)
            {
                endOfGame();
            }
        }

        private void C1_Click(object sender, EventArgs e)
        {
            if (C1beenClicked == 0)
            {
                if (turn % 2 == 0)
                    C1.Text = "O";
                else
                    C1.Text = "X";
                turn++;
                C1beenClicked = 1;
                if (vsComp == 1)
                {
                    computerChoice();
                }
            }

            if (turn > 9)
            {
                endOfGame();
            }
        }

        private void C2_Click(object sender, EventArgs e)
        {
            if (C2beenClicked == 0)
            {
                if (turn % 2 == 0)
                    C2.Text = "O";
                else
                    C2.Text = "X";
                turn++;
                C2beenClicked = 1;
                if (vsComp == 1)
                {
                    computerChoice();
                }
            }
            if (turn > 9)
            {
                endOfGame();
            }
        }

        private void C3_Click(object sender, EventArgs e)
        {
            if (C3beenClicked == 0)
            {
                if (turn % 2 == 0)
                    C3.Text = "O";
                else
                    C3.Text = "X";
                turn++;
                C3beenClicked = 1;
                if (vsComp == 1)
                {
                    computerChoice();
                }
            }

            if (turn > 9)
            {
                endOfGame();
            }
        }

        private void newGame_Click(object sender, EventArgs e)
        {
            startNewGame();
        }

        private void startNewGame()
        {
            A1.Text = "";
            A2.Text = "";
            A3.Text = "";
            B1.Text = "";
            B2.Text = "";
            B3.Text = "";
            C1.Text = "";
            C2.Text = "";
            C3.Text = "";
            turn = 1;
            A1beenClicked = 0;
            A2beenClicked = 0;
            A3beenClicked = 0;
            B1beenClicked = 0;
            B2beenClicked = 0;
            B3beenClicked = 0;
            C1beenClicked = 0;
            C2beenClicked = 0;
            C3beenClicked = 0;
            getPlayers();
        }
        int[] spaceValues = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
        private void convertToInt()
        {

            if (A1.Text == "X")
            {
                spaceValues[0] = 1;
            }
            else
            {
                spaceValues[0] = 0;
            }
            if (A2.Text == "X")
            {
                spaceValues[1] = 1;
            }
            else
            {
                spaceValues[1] = 0;
            }
            if (A3.Text == "X")
            {
                spaceValues[2] = 1;
            }
            else
            {
                spaceValues[2] = 0;
            }
            if (B1.Text == "X")
            {
                spaceValues[3] = 1;
            }
            else
            {
                spaceValues[3] = 0;
            }
            if (B2.Text == "X")
            {
                spaceValues[4] = 1;
            }
            else
            {
                spaceValues[4] = 0;
            }
            if (B3.Text == "X")
            {
                spaceValues[5] = 1;
            }
            else
            {
                spaceValues[5] = 0;
            }
            if (C1.Text == "X")
            {
                spaceValues[6] = 1;
            }
            else
            {
                spaceValues[6] = 0;
            }
            if (C2.Text == "X")
            {
                spaceValues[7] = 1;
            }
            else
            {
                spaceValues[7] = 0;
            }
            if (C3.Text == "X")
            {
                spaceValues[8] = 1;
            }
            else
            {
                spaceValues[8] = 0;
            }
        }
        private void endOfGame()
        {
            convertToInt();
            if (spaceValues[0] == spaceValues[1])
            {
                if (spaceValues[0] == spaceValues[3])
                {
                    MessageBox.Show(A1.Text + " is the winner!");
                }
            }
            if (spaceValues[3] == spaceValues[4])
            {
                if (spaceValues[3] == spaceValues[5])
                {
                    MessageBox.Show(B1.Text + " is the winner!");
                }
            }
            if (spaceValues[6] == spaceValues[7])
            {
                if (spaceValues[6] == spaceValues[8])
                {
                    MessageBox.Show(C1.Text + " is the winner!");
                }
            }
            if (spaceValues[0] == spaceValues[3])
            {
                if (spaceValues[0] == spaceValues[6])
                {
                    MessageBox.Show(A1.Text + " is the winner!");
                }
            }
            if (spaceValues[1] == spaceValues[4])
            {
                if (spaceValues[1] == spaceValues[7])
                {
                    MessageBox.Show(B1.Text + " is the winner!");
                }
            }
            if (spaceValues[2] == spaceValues[5])
            {
                if (spaceValues[2] == spaceValues[8])
                {
                    MessageBox.Show(C1.Text + " is the winner!");
                }
            }
            if (spaceValues[0] == spaceValues[4])
            {
                if (spaceValues[0] == spaceValues[8])
                {
                    MessageBox.Show(A1.Text + " is the winner!");
                }
            }
            if (spaceValues[2] == spaceValues[4])
            {
                if (spaceValues[2] == spaceValues[6])
                {
                    MessageBox.Show(C1.Text + " is the winner!");
                }
            }
        }
        /*string[] beenClicked = new string(9);
        string[] btnText;
        private void constructor()
        {
            beenClicked = { "A1beenClicked", "A2beenClicked", "A3beenClicked", "B1beenClicked", "B2beenClicked", "B3beenClicked", "C1beenClicked", "C2beenClicked", "C3beenClicked" };
            btnText = { A1.Text, A2.Text, A3.Text, B1.Text, B2.Text, B3.Text, C1.Text, C2.Text, C3.Text };
        }*/
        private void computerChoice()
        {
            /*constructor();
            int x = 0;
            while(x < 9)
            {*/
            if (/*beenClicked[x]*/A1beenClicked == 0)
            {
                /*btnText[x]*/A1.Text = "O";
                turn++;
                /*beenClicked[x]*/A1beenClicked = 1;
            }
        /*}
        }*/
        else
         {
             if (A2beenClicked == 0)
             {
                 A2.Text = "O";
                 turn++;
                 A2beenClicked = 1;
             }

             else
             {
                 if (A3beenClicked == 0)
                 {
                     A3.Text = "O";
                     turn++;
                     A3beenClicked = 1;
                 }
                 else
                 {
                     if (B1beenClicked == 0)
                     {
                         B1.Text = "O";
                         turn++;
                         B1beenClicked = 1;
                     }
                     else
                     {
                         if (B2beenClicked == 0)
                         {
                             B2.Text = "O";
                             turn++;
                             B2beenClicked = 1;
                         }
                         else
                         {
                             if (B3beenClicked == 0)
                             {
                                 B3.Text = "O";
                                 turn++;
                                 B3beenClicked = 1;
                             }
                             else
                             {
                                 if (C1beenClicked == 0)
                                 {
                                     C1.Text = "O";
                                     turn++;
                                     C1beenClicked = 1;
                                 }
                                 else
                                 {
                                     if (C2beenClicked == 0)
                                     {
                                         C2.Text = "O";
                                         turn++;
                                         C2beenClicked = 1;
                                     }
                                     else
                                     {
                                         if (C3beenClicked == 0)
                                         {
                                             C3.Text = "O";
                                             turn++;
                                             C3beenClicked = 1;
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
        private void getPlayers()
        {
            if (MessageBox.Show("Play  vs. the Computer?", "Players", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                vsComp = 1;
            }
            else
            {
                vsComp = 0;
            }
        }
    }
}